<template>
  <view class="commonLim-list">
    <!-- 表单内容 -->
    <view class="form-content flexandauto">
      <view class="form-item">
        <view>
          <text class="label" style="color: red">*</text>
          <text class="label">工单编号:</text>
        </view>
        <view class="flexCenter" style="flex: 1">
          <input
            class="iptClass textright"
            placeholder="请输入或扫工单编码"
            v-model.trim="profile.workOrderCode"
            style="width: 100%"
            @blur="handleblur1"
          />
          <image
            src="../../static/tabs/qr.png"
            @click="handlecan1"
            class="qrClass mgl10px"
          />
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">物料编码:</text>
        </view>
        <view>
          <text v-if="profile.materialCode" class="iptClass">{{
            profile.materialCode
          }}</text>
          <text class="dianwo" v-else>根据工单编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">材质:</text>
        </view>
        <view>
          <text v-if="profile.materialTypeName" class="iptClass">{{
            profile.materialTypeName
          }}</text>
          <text class="dianwo" v-else>根据工单编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">规格型号:</text>
        </view>
        <view>
          <text v-if="profile.specificationName" class="iptClass">{{
            profile.specificationName
          }}</text>
          <text class="dianwo" v-else>根据工单编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">炉号:</text>
        </view>
        <view>
          <text v-if="profile.heatNo" class="iptClass">{{
            profile.heatNo
          }}</text>
          <text class="dianwo" v-else>根据工单编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">生产量:</text>
        </view>
        <view>
          <text v-if="profile.actualWeight" class="iptClass">{{
            profile.actualWeight
          }}</text>
          <text class="dianwo" v-else>根据工单编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">分轮数:</text>
        </view>
        <view>
          <text v-if="profile.plannedSplitCount" class="iptClass">{{
            profile.plannedSplitCount
          }}</text>
          <text class="dianwo" v-else>根据工单编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">分轮重量:</text>
        </view>
        <view>
          <text v-if="profile.splitWeight" class="iptClass">{{
            profile.splitWeight
          }}</text>
          <text class="dianwo" v-else>根据工单编号带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label" style="color: red">*</text>
          <text class="label">重量:</text>
        </view>
        <view class="flexCenter" style="flex: 1">
          <input
            class="iptClass textright"
            placeholder="请输入重量"
            v-model.trim="roundWeighingWeight"
            type="number"
            style="width: 100%"
          />
          <text>kg</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label" style="color: red">*</text>
          <text class="label">编号:</text>
        </view>
        <view class="flexCenter" style="flex: 1">
          <input
            class="iptClass textright"
            placeholder="输入或扫原料编码/半成品物料编码"
            v-model.trim="trackingNo"
            style="width: 100%"
            @blur="handleblur2"
          />
          <image
            src="../../static/tabs/qr.png"
            @click="handlecan2"
            class="qrClass mgl10px"
          />
        </view>
      </view>

      <view class="form-item">
        <view>
          <text class="label">材质:</text>
        </view>
        <view>
          <text v-if="tracking.materialTypeName" class="iptClass">{{
            tracking.materialTypeName
          }}</text>
          <text class="dianwo" v-else>根据扫码带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">规格型号:</text>
        </view>
        <view>
          <text v-if="tracking.specificationName" class="iptClass">{{
            tracking.specificationName
          }}</text>
          <text class="dianwo" v-else>根据扫码带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">炉号:</text>
        </view>
        <view>
          <text v-if="tracking.heatNo" class="iptClass">{{
            tracking.heatNo
          }}</text>
          <text class="dianwo" v-else>根据扫码带出</text>
        </view>
      </view>
      <view class="form-item">
        <view>
          <text class="label">卷号:</text>
        </view>
        <view>
          <text v-if="tracking.coilNo" class="iptClass">{{
            tracking.coilNo
          }}</text>
          <text class="dianwo" v-else>根据扫码带出</text>
        </view>
      </view>
    </view>
    <!-- 提交按钮 -->
    <view class="dengjiClass">
      <button @click="onSubmit" class="commonClass">保存</button>
    </view>
  </view>
</template>

<script setup>
import { materialReturnSumit, saomarequest1, saomarequest2 } from "./api.js";
import { useMemberStore } from "@/stores/modules/member";
import { onCan } from "@/utils/index.js";
const memberStore = useMemberStore();
const deprops = defineProps(["title"]);
const roundWeighingWeight = ref("");
const trackingNo = ref("");
const profile = ref({});
const tracking = ref({});
// 点击提交表单
const onSubmit = async () => {
  try {
    if (!profile.value.workOrderCode) {
      return uni.showToast({
        icon: "none",
        title: "请输入或扫工单编码",
      });
    }
    if (!+roundWeighingWeight.value) {
      return uni.showToast({
        icon: "none",
        title: "重量不能为空或0",
      });
    }
    if (!trackingNo.value) {
      return uni.showToast({
        icon: "none",
        title: "请输入或扫原料编码/半成品物料编码",
      });
    }
    uni.showLoading({
      title: "加载中",
    });
    console.log(tracking, "9999999");
    await materialReturnSumit({
      createBy: memberStore.userId,
      actualSplitCount: profile.value.actualSplitCount,
      heatNo: profile.value.heatNo,
      materialCode: profile.value.materialCode,
      materialTypeId: profile.value.materialTypeId,
      materialTypeName: profile.value.materialTypeName,
      plannedSplitCount: profile.value.plannedSplitCount,
      producedQuantity: profile.value.actualWeight,
      roundWeighingWeight: +roundWeighingWeight.value,
      sourceNo: trackingNo.value,
      sourceNoId: tracking.value.id,
      specificationId: profile.value.specificationId,
      specificationName: profile.value.specificationName,
      splitWeight: profile.value.splitWeight,
      workOrderCode: profile.value.workOrderCode,
      workOrderId: profile.value.id,
    });
    uni.showToast({ icon: "success", title: "保存成功" });
    uni.navigateBack({ delta: 1 });
  } finally {
    uni.hideLoading();
  }
};
const fengzhuang1 = async (value) => {
  const res = await saomarequest1(value);
  if (res) {
    profile.value = res;
  }
};
const fengzhuang2 = async (value) => {
  trackingNo.value = value;
  const res = await saomarequest2(value);
  if (res) {
    tracking.value = res;
  }
};
const handleblur1 = async (result) => {
  const value = result.detail.value;
  if (value) {
    fengzhuang1(value);
  }
};
const handleblur2 = async (result) => {
  const value = result.detail.value;
  if (value) {
    fengzhuang2(value);
  }
};

const handlecan1 = () => {
  onCan(fengzhuang1);
};
const handlecan2 = () => {
  onCan(fengzhuang2);
};
onLoad(async () => {
  uni.setNavigationBarTitle({
    title: deprops.title,
  });
});

onShow(() => {});
</script>

<style lang="scss" scoped>
</style>
